package tuio;

import gui.visu.components.A_DisplayData;

import data.visu.DisplayDataList;

/**
 * @author Charles Perin
 * 29 mars 2011
 * LIMSI - INRIA Rennes
 * charles.perin@free.fr
 *
 */
public class ContinueMvt{

	private static final int TIME_TO_SLEEP = 20;
	private float initSpeed;
	private DisplayDataList displayDataList;

	public ContinueMvt(float initSpeed, A_DisplayData a_DisplayData){
		this.initSpeed=initSpeed;
		this.displayDataList=a_DisplayData.getControler().getDisplayDataList();
	}

	public void start() {
		float speed = initSpeed;
		int xMax = displayDataList.getxMax();
		int xMin = displayDataList.getxMin();
		int screenWidth = displayDataList.getDataControl().getIhm().getWidth();
		int newXMaxView = (int)displayDataList.getxMaxView();
		int newXMinView = (int)displayDataList.getxMinView();
		if(speed>0){
			int cpt = 0;
			while(speed>0.0001 && newXMaxView!=xMax){
				System.out.println("cpt="+cpt);
				cpt++;
				newXMinView = (int)(displayDataList.getxMinView()+speed*screenWidth);
				newXMaxView = (int)(displayDataList.getxMaxView()+speed*screenWidth);
				speed = 4*speed/5.0f;
				if(newXMaxView>xMax){
					newXMinView=xMax-(newXMaxView-newXMinView);
					newXMaxView=xMax;
					displayDataList.setXView(newXMinView, newXMaxView);
					break;
				}
				displayDataList.setXView(newXMinView, newXMaxView);
				try {
					Thread.sleep(TIME_TO_SLEEP);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
		else if (speed<0){
			while(speed<-0.0001 && newXMinView!=xMin){
				newXMinView = (int)(displayDataList.getxMinView()+speed*screenWidth);
				newXMaxView = (int)(displayDataList.getxMaxView()+speed*screenWidth);
				speed = 4*speed/5.0f;
				if(newXMinView<xMin){
					newXMaxView = xMin+(newXMaxView-newXMinView);
					newXMinView=xMin;
					displayDataList.setXView(newXMinView, newXMaxView);
					break;
				}
				displayDataList.setXView(newXMinView, newXMaxView);
				try {
					Thread.sleep(TIME_TO_SLEEP);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
